const inquirer = require('inquirer')
const fs = require('fs');
const path = require('path'); //解析需要遍历的文件夹
const inImagePath = path.resolve('./src/images');
const pluginaPath = path.resolve('./src/template/components/plugin');

// 携带指定插件资源的方法
function fgetPlugin(area, type) {

    let tar = path.join('./src/area/', area + '.ejs')
    // 清空错误的写入
    if (type === "clean") {
        fs.writeFileSync(tar, '')
        return;
    }
    //找到插件的位置
    let plugin_dir = './src/template/components/plugin/' + type
    //把需要的html引入
    let html_filename = path.join(plugin_dir, 'html.ejs')
    let str = fs.readFileSync(html_filename).toString();
    fs.writeFileSync(tar, str, function (err) {
        err && console.log('写入html失败')
    })

    //把需要的link引入
    let link_filename = path.join(plugin_dir, 'link.ejs')
    let link_data = fs.readFileSync(link_filename).toString();
    let all_link = path.join(plugin_dir, '..', 'all_link.ejs')
    //link排重
    if (fs.readFileSync(all_link).toString().indexOf(link_data) == -1) {
        fs.appendFileSync(all_link, link_data, function (err) {
            if (err) console.log('写入all_link失败')
        });
    };

    //把需要的css写入
    let css_filename = path.join(plugin_dir, 'style.css')
    let css_data = fs.readFileSync(css_filename).toString();
    // console.log(link_data)
    let main_css = './src/css/style.css'
    //css排重
    if (fs.readFileSync(main_css).toString().indexOf(css_data) == -1) {
        fs.appendFileSync(main_css, '\n' + css_data, function (err) {
            if (err) console.log('写入all_link失败')
        });
    };

    //把需要的script引入
    let script_filename = path.join(plugin_dir, 'script.ejs')
    let script_data = fs.readFileSync(script_filename).toString();
    let all_script = path.join(plugin_dir, '..', 'all_script.ejs')
    //script排重
    if (fs.readFileSync(all_script).toString().indexOf(script_data) == -1) {
        fs.appendFileSync(all_script, script_data, function (err) {
            if (err) console.log('写入all_script失败')
        });
    };

    //把需要的js写入
    let js_filename = path.join(plugin_dir, 'main.js')
    let js_data = fs.readFileSync(js_filename).toString();
    let index_js = './src/js/main/index.js'
    //js排重
    if (fs.readFileSync(index_js).toString().indexOf(js_data) == -1) {
        fs.appendFileSync(index_js, '\n' + js_data, function (err) {
            if (err) console.log('写入index_js失败')
        });
    };

}

const fileArr=[]
fs.readdir(pluginaPath, function (err, files) {
    files.map(item => {
        let itemPath = path.resolve(pluginaPath, item)
        if(fs.statSync(itemPath).isDirectory()){
            fileArr.push(item)
        }
    })
    fileArr.push('clean')
})

fs.readdir(inImagePath, function (err, files) {
        files = files.map(item => {
            return item.slice(0, -4)
        })
        frequest(files)
})

function frequest(files) {
    // console.log(files)
    inquirer
        .prompt([
            {
                type: 'list',
                name: 'area',
                message: '选择视图上组件所在的位置',
                choices: files,
            },
            {
                type: 'list',
                name: 'type',
                message: '选择组件的类型(clean为清空错误的写入)',
                choices: fileArr,
            }
        ])
        .then(answers => {
            console.log(answers.area, answers.type)
            let area = answers.area, type = answers.type;
            fgetPlugin(area, type)
        })
}

